package com.example.query.sdcard;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.query.sdcard.ListAdapter.ViewHolder;

public class MainActivity extends Activity {

	public String path = "/sdcard";
	public String prevPath = "/";
	public ArrayList<Item> listItem = new ArrayList<Item>();
	public Item item;
	public ListView listView;
	public ImageButton imgButton;
	public TextView tvPath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		imgButton = (ImageButton) findViewById(R.id.back_button);
		imgButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View view) {
				// TODO Auto-generated method stub								
				backListItem(prevPath);
			}
		});
		
		tvPath = (TextView) findViewById(R.id.path_label);
		tvPath.setText("Path is : " + path);
		
		listView = (ListView) findViewById(R.id.listItem);		
		
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int pos, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "" + pos, Toast.LENGTH_SHORT).show();
				return true;
			}
		});
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int pos,
					long id) {
				// TODO Auto-generated method stub
				ViewHolder holder = (ViewHolder) view.getTag();
				if (holder == null) {
					return;
				}
				
				String _path = (String) holder.title.getContentDescription();
				File f = new File(_path);
				if (f.canRead()) {
					if (f.isDirectory()) {
						prevPath = path;					
						path = _path;										
						onResume();
					} else {
						Toast.makeText(getBaseContext(), "This is a file", Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(getBaseContext(), "Can't read folder due to permissions", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();	
		tvPath.setText("Path is : " + path);
		initListItem(path);
	}

	private void initListItem(String path) {
		listItem.clear();
		File f = new File(path);
		if (f.exists()) {
			File[] files = f.listFiles();			
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					item = new Item(true, files[i].getName(), files[i].getAbsolutePath());
				} else {
					item = new Item(false, files[i].getName(), files[i].getAbsolutePath());
				}
				listItem.add(item);
			}
		}
		
		listView.setAdapter(new ListAdapter(this, listItem));
	}
	
	private void backListItem(String _prevPath) {
		this.path = _prevPath;
		this.prevPath = _prevPath.substring(0, _prevPath.lastIndexOf("/"));
		if (prevPath.equals("")) {
			prevPath = "/";
		}		
		onResume();
	}

}